export default class VerityCode {
    constructor(element, options) {
        this.$element = $(element);
        this.options = Object.assign({}, options);
        this.timer = null;
        this.timeCount = -1;
        this.mobile = '';
        this.code = '';
        this.init();
    }

    init() {

    }

    get isDisabled() {
        return this.$element.prop('disabled')
    }

    validMobileCode() {
        return this.mobile !== '' && this.code !== '';
    }

    ajax() {
        const url = this.options.url;
        return new Promise((resolve, reject) => {
            $.ajax({
                type: 'POST',
                url: url,
                data: {
                    mobile: this.mobile,
                    code: this.code
                }
            }).done((data)=>{
                resolve(data)
            }).catch((error)=>{
                reject(error)
            })
        })
    }

    handleChangeCode() {

        this.$element.on('click', (e) => {
            e.stopPropagation();
            e.preventDefault();
            if (this.isDisabled) {
                return false;
            }
            if (!this.validMobileCode()) {
                throw Error('还未填写手机号或者验证码');
            }

        })
    }
}